package cn.zy.init;


import cn.lj.common.redis.constant.RedisConstant;
import cn.lj.dict.service.bo.SmallMiDictBO;
import cn.lj.dict.service.dos.SmallMiDictDO;
import cn.lj.dict.service.service.DictService;
import cn.zy.common.redis.RedisServie;
import cn.zy.register.mapper.pojo.SmallMiUser;
import cn.zy.register.service.SmallMiUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Configuration
public class InitData implements InitializingBean {

    Logger logger = LoggerFactory.getLogger(InitData.class);
    //实例redis
    @Autowired
    private RedisServie redisServie;
    //实例用户表service
    @Autowired
    private SmallMiUserService service;
    //实例字段表service
    @Autowired
    private DictService dictService;

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("开始加载初始化数据！");
        initdataDict();
        initdataDicts();
        System.out.println("结束加载初始化数据！");
    }

    /**
     * 初始化用户数据
     */
    public void initdataDict() {
        List<SmallMiUser> all = service.getAll();
        Map<String, SmallMiUser> map = all.stream().collect(Collectors.toMap(SmallMiUser::getMi_phone, SmallMiUser -> SmallMiUser));
        redisServie.putHashAll(DataConstant.USER_DATA, map);

    }

    /**
     * 功能描述: 初始化字典值数据
     * 添加字典表数据直接进入这个方法添加就可以
     *
     * @auther: lijie
     * @date: 2021/7/17 上午 10:04:4
     * @return: void
     */
    public void initdataDicts() {
        //创建对象
        SmallMiDictBO smallMiDictBO = new SmallMiDictBO();
        //把userStateType传入字典值
        smallMiDictBO.setGroup_key("userStateType");
        List<SmallMiDictDO> userStateType = dictService.groupkeys(smallMiDictBO);
        Map<String, SmallMiDictDO> userStateTypeMap = userStateType.stream().collect(Collectors.toMap(SmallMiDictDO::getDict_key, SmallMiDictDO -> SmallMiDictDO));
        redisServie.putHashAll(RedisConstant.DRCT_USERSTATETYPE, userStateTypeMap);
        //把discountType传入字典值
        smallMiDictBO.setGroup_key("discountType");
        List<SmallMiDictDO> discountType = dictService.groupkeys(smallMiDictBO);
        Map<String, SmallMiDictDO> discountTypeMap = discountType.stream().collect(Collectors.toMap(SmallMiDictDO::getDict_key, SmallMiDictDO -> SmallMiDictDO));
        redisServie.putHashAll(RedisConstant.DRCT_DISCOUNTTYPE, discountTypeMap);
        //把discountState传入字典值
        smallMiDictBO.setGroup_key("discountState");
        List<SmallMiDictDO> discountState = dictService.groupkeys(smallMiDictBO);
        Map<String, SmallMiDictDO> discountStateMap = discountState.stream().collect(Collectors.toMap(SmallMiDictDO::getDict_key, SmallMiDictDO -> SmallMiDictDO));
        redisServie.putHashAll(RedisConstant.DRCT_DISCOUNTSTATE, discountStateMap);
        //把orderState传入字典值
        smallMiDictBO.setGroup_key("orderState");
        List<SmallMiDictDO> orderState = dictService.groupkeys(smallMiDictBO);
        Map<String, SmallMiDictDO> orderStateMap = orderState.stream().collect(Collectors.toMap(SmallMiDictDO::getDict_key, SmallMiDictDO -> SmallMiDictDO));
        redisServie.putHashAll(RedisConstant.DRCT_ORDERSTATE, orderStateMap);
        //把msgType传入字典值
        smallMiDictBO.setGroup_key("msgType");
        List<SmallMiDictDO> msgType = dictService.groupkeys(smallMiDictBO);
        Map<String, SmallMiDictDO> msgTypeMap = msgType.stream().collect(Collectors.toMap(SmallMiDictDO::getDict_key, SmallMiDictDO -> SmallMiDictDO));
        redisServie.putHashAll(RedisConstant.DRCT_MSGTYPE, msgTypeMap);
    }
}
